<?php

class OptionsImageController extends Controller {
    var $name = 'OptionsImage';
    var $uses = array('Options');
    var $components = array('options');
    
    function index() {
        $this->action = 'options_image';
        $this->options_image();
    }
    
    function options_image($status = null) {
        if (is_readable(ROOT . 'www/watermark.gif')) {
            $watermark_file_exists = true;
        } else {
            $watermark_file_exists = false;
        }
        if (!empty($this->data['Options'])) {
            # set the thumbnail status
            if ($this->data['Options']['thumbnail_status'] == 0) {
                $this->data['Options']['is_thumbnail'] = 0;
            } else {
                $this->data['Options']['is_thumbnail'] = 1;
            }
            # set the watermark status
            if (empty($this->data['Options']['is_watermark']) || !$watermark_file_exists) {
                $this->data['Options']['is_watermark'] = 0;
            }
            foreach($this->data['Options'] as $key => $value) {
                update_option($key, $value);
            }
            $this->redirect('/options_image/options_image/update_done');
            exit();
        }
        $options = $this->Options->find_all('',array('option_name', 'option_value'));
        $thumbnail_display_style = 'display:none;';
        $watermark_display_style = 'display:none;';
        foreach($options as $option) {
            $this->data['Options'][$option->option_name] = $option->option_value;
            if ($option->option_name == 'is_thumbnail' && $option->option_value == 1) {
                $thumbnail_display_style = '';
            }
            if ($option->option_name == 'is_watermark' && $option->option_value == 1) {
                $watermark_display_style = '';
            }
        }

        $this->set('watermark_file_exists', $watermark_file_exists);
        $this->set('thumbnail_display_style', $thumbnail_display_style);
        $this->set('watermark_display_style', $watermark_display_style);
        if ($status == 'update_done') {
            $this->header_notice = __('Options saved.');
        }
    }
}

?>
